package com.h2cloud.copy;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;

/**
 * @author: wulongkai
 * @create: 2022-11-01 11:01
 **/
@Data
@AllArgsConstructor
public class BaseInfo {
    public static void main(String[] args) {
//        String s = "beforeBaseSalary";
//        String afterFieldName =s.substring(6, 7).toLowerCase() + s.substring(7);
//        System.out.println("afterFieldName = " + afterFieldName);

/**
 * 解析没有数据头的纯数组
 */

        String strByJson = "{\"muser\":[{\"name\":\"zhangsan\",\"age\":\"10\",\"phone\":\"11111\",\"email\":\"11111@11.com\"},{\"name\":\"zhangsan\",\"age\":\"20\",\"phone\":\"11111\",\"email\":\"11111@11.com\"}]}";
        //先转JsonObject
        JsonObject jsonObject = JsonParser.parseString(strByJson).getAsJsonObject();
        //再转JsonArray 加上数据头
        JsonArray jsonArray = jsonObject.getAsJsonArray("muser");

        Gson gson = new Gson();
        ArrayList<UserBean> userBeanList = new ArrayList<>();

        //循环遍历
        for (JsonElement user : jsonArray) {
            //通过反射 得到UserBean.class
            UserBean userBean = gson.fromJson(user, new TypeToken<UserBean>() {
            }.getType());
            userBeanList.add(userBean);
        }
        System.out.println("userBeanList = " + userBeanList);

    }

    public static boolean isPreEmploymentType(int value) {
        return value == 1;
    }
}

@Data
class UserBean {
    //变量名跟JSON数据的字段名需要一致
    private String name;
    private String age;
    private String phone;
    private String email;
}

@Data
@AllArgsConstructor
class obj {
    int i;
}
